package org.dromara.tearoom.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerIotDeviceOperLogBo;
import org.dromara.tearoom.domain.vo.TerIotDeviceOperLogVo;
import org.dromara.tearoom.domain.TerIotDeviceOperLog;
import org.dromara.tearoom.mapper.TerIotDeviceOperLogMapper;
import org.dromara.tearoom.service.ITerIotDeviceOperLogService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * IoT设备操作日志Service业务层处理
 *
 * @author ckj
 * @date 2025-04-17
 */
@RequiredArgsConstructor
@Service
public class TerIotDeviceOperLogServiceImpl implements ITerIotDeviceOperLogService {

    private final TerIotDeviceOperLogMapper baseMapper;

    /**
     * 查询IoT设备操作日志
     *
     * @param id 主键
     * @return IoT设备操作日志
     */
    @Override
    public TerIotDeviceOperLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询IoT设备操作日志列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return IoT设备操作日志分页列表
     */
    @Override
    public TableDataInfo<TerIotDeviceOperLogVo> queryPageList(TerIotDeviceOperLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerIotDeviceOperLog> lqw = buildQueryWrapper(bo);
        Page<TerIotDeviceOperLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的IoT设备操作日志列表
     *
     * @param bo 查询条件
     * @return IoT设备操作日志列表
     */
    @Override
    public List<TerIotDeviceOperLogVo> queryList(TerIotDeviceOperLogBo bo) {
        LambdaQueryWrapper<TerIotDeviceOperLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerIotDeviceOperLog> buildQueryWrapper(TerIotDeviceOperLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TerIotDeviceOperLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceKey()), TerIotDeviceOperLog::getDeviceKey, bo.getDeviceKey());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TerIotDeviceOperLog::getType, bo.getType());
        lqw.orderByDesc(TerIotDeviceOperLog::getCreateTime);
        return lqw;
    }

    /**
     * 新增IoT设备操作日志
     *
     * @param bo IoT设备操作日志
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerIotDeviceOperLogBo bo) {
        TerIotDeviceOperLog add = MapstructUtils.convert(bo, TerIotDeviceOperLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改IoT设备操作日志
     *
     * @param bo IoT设备操作日志
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerIotDeviceOperLogBo bo) {
        TerIotDeviceOperLog update = MapstructUtils.convert(bo, TerIotDeviceOperLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerIotDeviceOperLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除IoT设备操作日志信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 校验并批量删除IoT设备操作日志信息
     *
     * @param deviceKeys 待删除的设备编号
     * @param isValid    是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteBatchByDeviceKeys(Collection<String> deviceKeys, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        LambdaQueryWrapper<TerIotDeviceOperLog> lqw = Wrappers.lambdaQuery();
        lqw.in(CollUtil.isNotEmpty(deviceKeys),TerIotDeviceOperLog::getDeviceKey, deviceKeys);
        return baseMapper.delete(lqw) > 0;
    }

    /**
     * 校验并批量删除IoT设备操作日志信息
     *
     * @param localDateTime 时间
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteBatchByTime(LocalDateTime localDateTime) {
        return baseMapper.delete(new LambdaQueryWrapper<TerIotDeviceOperLog>().le(TerIotDeviceOperLog::getCreateTime, localDateTime)) > 0;
    }
}
